﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;

namespace DAO
{
    public class PhieuNhapSachDAO
    {
        static public DataTable SelectPhieuNhapAll()
        {
            string sql = "select* from PHIEUNHAP";
            DataTable dt = DataAccess.ExcuQuery(sql);
            return dt;
        }
        static public DataTable SelectPhieuNhapChiTietAll()
        {
            string sql = "select ctpn.MaPhieuNhap,ctpn.MaSach,ctpn.SoLuongNhap from CHITIETPHIEUNHAP ctpn";
            DataTable dt = DataAccess.ExcuQuery(sql);
            return dt;
        }
        static public void Insert(PhieuNhapSachDTO p)
        {
            string sql = "insert into CHITIETPHIEUNHAP(MaPhieuNhap,MaSach,SoLuongNhap) values(" + p.MaPhieuNhap + "," + p.MaSach + "," + p.SoLuong + ")";
            DataAccess.ExcuNonQuery(sql);
        }
        static public void Insertphieunhap(PhieuNhapSachDTO p)
        {
            string sql = "insert into PHIEUNHAP(NgayNhap) values('" + p.NgayNhap + "')";
            DataAccess.ExcuNonQuery(sql);
        }
        public static PhieuNhapSachDTO GetPhieuNhapByName(int maphieunhap, int masach)
        {
            string sql = "select * from CHITIETPHIEUNHAP where ((MaPhieuNhap=" + maphieunhap + ")AND(MaSach = " + masach + "))";
            DataTable dt = DataAccess.ExcuQuery(sql);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                PhieuNhapSachDTO pn = new PhieuNhapSachDTO();
                pn.MaPhieuNhap = (int)dt.Rows[0].ItemArray[0];
                return pn;
            }

        }
    }
}
